﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CuaHangBanGiay.Models;
using CuaHangBanGiay._Library;
using CuaHangBanGiay.Controllers;

namespace CuaHangBanGiay.Controllers
{
    public class SanPhamController : Controller
    {
        //
        // GET: /SanPham/
        public CuaHangBanGiayDBContext _db;
        public SanPhamController(CuaHangBanGiayDBContext _db)
        {
            this._db = _db;
        }
        public SanPhamController()
        {
            this._db = new CuaHangBanGiayDBContext();
        }
        public SanPham get_by_id(int id)
        {
            return _db.ds_sanpham.FirstOrDefault(x => x.id == id);
        }
        public Boolean is_exist(int id)
        {
            SanPham u = (from sp in _db.ds_sanpham
                             where sp.id == id
                             select sp).FirstOrDefault();
            return u == null ? false : true;
        }
        public int add(SanPham sp)
        {
            this._db.ds_sanpham.Add(sp);
            this._db.SaveChanges();
            return this._db.ds_sanpham.Max(x => x.id);
        }
        public Boolean edit(SanPham sp)
        {
            if(this.is_exist(sp.id))
            {
                SanPham obj = this.get_by_id(sp.id);
                obj.mota = sp.mota;
                obj.nhomsanpham = sp.nhomsanpham;
                obj.gia = sp.gia;
                obj.hangsx = sp.hangsx;
                obj.ten = sp.ten; ;
                _db.SaveChanges();
                return true;
            }
            return false;
        }
        public Boolean delete(int id)
        {
            if (this.is_exist(id))
            {
                SanPham sp = this.get_by_id(id);
                foreach (ChiTietSP spt in sp.ds_sanpham_tag)
                {
                    ChiTietSPController ctrl = new ChiTietSPController(this._db);
                    if(!ctrl.delete(spt.id)) return false;
                }
                foreach (HinhAnh hinh in sp.ds_hinhanh)
                {
                    HinhAnhController ctrl = new HinhAnhController(this._db);
                    if(!ctrl.delete(hinh.id)) return false;
                }
                _db.ds_sanpham.Remove(sp);
                _db.SaveChanges();
                return true;
            }
            return false;
        }
        public List<SanPham> timkiem(String id = "",String masp="", String ten = "", String id_hsx = "", String id_nhom = "", String active = "", Boolean order_des = true, int start = 0, int count = -1)
        {
            List<SanPham> list = _db.ds_sanpham.ToList();
            if (!id.Equals(""))
            {
                int id_sp = TextLibrary.ToInt(id);
                list = _db.ds_sanpham.Where(x => x.id == id_sp).ToList();
                if (list == null)
                {
                    return new List<SanPham>();
                }
                return list;
            }
            if (list == null)
            {
                list = new List<SanPham>();
            }
            list = list.Where(x => x.ten.ToUpper().Contains(ten.ToUpper()) && x.masp.ToUpper().Contains(masp.ToUpper())).ToList();
            if (list == null)
            {
                list = new List<SanPham>();
            }
            if (!id_hsx.Equals(""))
            {
                int idhsx = TextLibrary.ToInt(id_hsx);
                list = list.Where(x => x.hangsx.id == idhsx).ToList();
                if (list == null)
                {
                    return new List<SanPham>();
                }
            }
            if (!id_nhom.Equals(""))
            {
                int idnhom = TextLibrary.ToInt(id_nhom);
                list = list.Where(x => x.nhomsanpham.id == idnhom).ToList();
                if (list == null)
                {
                  list= new List<SanPham>();
                }
            }
            if (!active.Equals(""))
            {
                Boolean active_b = TextLibrary.ToBoolean(active);
                list = list.Where(x => x.active == active_b).ToList();
                if (list == null)
                {
                    list = new List<SanPham>();
                }
            }
            if (order_des)
                list = list.OrderByDescending(x => x.id).ToList();
            else
                list = list.OrderBy(x => x.id).ToList();
            if (list == null) list = new List<SanPham>();
            if (count >= 0)
            {
                list = list.Skip(start).Take(count).ToList();
                if (list == null) list = new List<SanPham>();
            }
            return list; 

        }
    }
}
